home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / emulation / zip2d64 / source / zip2d64.c
C/C++ Source or Header  |  1999-05-14  |  4KB  |  173 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. FILE *infile;
  5. FILE *outfile;
  6. unsigned char track_data[8193];
  7. unsigned char sector_flag[21];
  8. char filename[1024];
  9. unsigned sector, max_sector;
  10.  
  11. void fileerror()
  12.  
  13. {
  14.     printf("File is corrupted.\n\r");
  15.     exit(1);
  16. }
  17.  
  18. void error()
  19.  
  20. {
  21.     fclose(infile);
  22.     fclose(outfile);
  23.     exit(1);
  24. }
  25.  
  26. void error2()
  27.  
  28. {
  29.     fclose(outfile);
  30.     exit(1);
  31. }
  32.  
  33. void fillmemory(unsigned char *buffer, unsigned int bytes, unsigned char filler)
  34.  
  35. {
  36.     int x;
  37.     for (x=0; x<bytes; x++)
  38.         buffer[x]=filler;
  39. }
  40.  
  41. unsigned short int read_sector(FILE *f, unsigned char track)
  42.  
  43. {
  44.     unsigned char trk, sec, len, rep, repnum, chra;
  45.     unsigned short int i,j,count;
  46.     fread(&trk,1,1,f);
  47.     fread(&sec,1,1,f);
  48.     if (((trk&63)!=track)||(sector_flag[sec]!=0)||(sec>max_sector)) 
  49.         fileerror();
  50.     sector_flag[sec]=1;
  51.     if ((trk&128)==128) {
  52.         printf("1");
  53.         fread(&len,1,1,f);
  54.         fread(&rep,1,1,f);
  55.         count=0;
  56.         for (i=0; i<len; i++) {
  57.             if (feof(f)) 
  58.                 error();
  59.             fread(&chra,1,1,f);
  60.             if (chra!=rep) {
  61.                 track_data[(sec<<8)+count]=chra;
  62.                 count++;
  63.             } 
  64.             else {
  65.                 fread(&repnum,1,1,f);
  66.                 if (feof(f)) 
  67.                     fileerror();
  68.                 fread(&chra,1,1,f);
  69.                 i=i+2;
  70.                 for (j=0; j<repnum; j++) {
  71.                     track_data[(sec<<8)+count]=chra;
  72.                     count++;
  73.                 }
  74.             }
  75.         }
  76.     }
  77.     else if ((trk&64)==64) {
  78.         printf("2");
  79.         if (feof(f)) 
  80.             fileerror();
  81.         fread(&chra,1,1,f);
  82.         fillmemory((unsigned char *)&track_data[sec<<8],256,chra);
  83.     } 
  84.     else {
  85.         printf("3");
  86.         i=fread(&track_data[sec<<8],256,1,f);
  87.         if (i!=1)
  88.             fileerror();
  89.     }
  90.     return 0;
  91. }
  92.  
  93. unsigned short int openfile(unsigned char p, char *name)
  94.  
  95. {
  96.     if (p>1)  
  97.         fclose(infile);
  98.     filename[0]=p+48;
  99.     filename[1]='!';
  100.     strcpy(&filename[2],name);
  101.     infile=fopen(filename,"rb");
  102.     if (infile==NULL) {
  103.         printf("Error opening file\n\r");
  104.         return 1;
  105.     }
  106.     if (p==1)
  107.         fseek(infile,4,SEEK_SET);
  108.     else
  109.         fseek(infile,2,SEEK_SET);
  110.     return 0;
  111. }
  112.  
  113. //label Error, Error2;
  114.  
  115. int main(int argc, char *argv[])
  116.  
  117. {
  118.     unsigned short int block_count;
  119.     unsigned char track;
  120.     printf("\n\rZipCode 2 D64 - Extract Zipcode disks to .D64 images.\n\rModified by duet <duet@sci.fi>.\n\r");
  121.     if (argc!=3) {
  122.         printf("Usage: zip2d64 <zipcode file> <d64 image>\n\r");
  123.         exit(1);
  124.     }  
  125.     outfile=fopen(argv[2],"wb");
  126.     if (outfile==NULL)
  127.         fileerror();    
  128.     block_count=0;
  129.     for (track=1; track<36; track++) {
  130.         fillmemory((unsigned char *)&track_data,sizeof(track_data),0);
  131.         if ((track>=1) && (track<=17)) 
  132.             max_sector=20;
  133.         if ((track>=18) && (track<=24)) 
  134.             max_sector=18;
  135.         if ((track>=25) && (track<=30)) 
  136.             max_sector=17;
  137.         if ((track>=31) && (track<=35)) 
  138.             max_sector=16;
  139.       switch(track) {
  140.           case 1: 
  141.               if (openfile(1,argv[1])!=0) 
  142.                   error2();
  143.               break;
  144.           case 9: 
  145.               if (openfile(2,argv[1])!=0) 
  146.                   error2();
  147.               break;
  148.           case 17: 
  149.               if (openfile(3,argv[1])!=0)
  150.                   error2();
  151.               break;
  152.           case 26: 
  153.               if (openfile(4,argv[1])!=0) 
  154.                   error2();
  155.               break;
  156.           default:
  157.               break;
  158.       }
  159.       fillmemory((unsigned char *)§or_flag,sizeof(sector_flag),0);
  160.       printf("Track %i ",track);
  161.       for (sector=0; sector<max_sector+1; sector++) {
  162.           block_count++;
  163.           if (read_sector(infile,track)!=0) 
  164.               error();
  165.       }
  166.       fwrite(&track_data,max_sector*256+256,1,outfile);
  167.       printf("\n\r");
  168.     }
  169.     fclose(infile);
  170.     fclose(outfile);
  171.     return 0;
  172. }
  173.